package com.whiteape.book.ui.adapter;

import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.whiteape.book.R;
import com.whiteape.book.core.manager.MediaStorageManager;
import com.whiteape.book.model.bo.CollectionMediaWrapper;
import com.whiteape.book.model.dto.CollectionMediaBean;
import com.whiteape.book.model.dto.MediaChapterBean;
import com.whiteape.book.model.dto.MediaRecordBean;
import com.whiteape.book.model.type.RVLayoutType;
import com.whiteape.book.utils.DaoDbHelper;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MediaCollectionChildGridAdapter extends BaseMultiItemQuickAdapter<CollectionMediaWrapper, BaseViewHolder> {

    private List<CollectionMediaWrapper> selectedList = new ArrayList<>();
    private RVLayoutType layoutType = RVLayoutType.LIST;
    private boolean selectedStateEnabled = false;
    private Set<String> refreshingMediaSet = new HashSet<>();

    public MediaCollectionChildGridAdapter(@Nullable List<CollectionMediaWrapper> data) {
//        super(layoutResId, data);
        super(data);
        addItemType(CollectionMediaWrapper.COMMON, R.layout.item_collection_media_grid);
        addItemType(CollectionMediaWrapper.GROUP, R.layout.item_collection_media_group_detail_grid);
    }

    public void addRefreshingMedia(String media) {
        refreshingMediaSet.add(media);
    }

    public void removeRefreshingMedia(String media) {
        refreshingMediaSet.remove(media);
    }

    public void setLayoutType(RVLayoutType layoutType) {
        this.layoutType = layoutType;
    }

    public void setSelectedList(List<CollectionMediaWrapper> beanList) {
        selectedList = beanList;
    }

    public void setSelectedStateEnable(boolean enable) {
        selectedStateEnabled = enable;
    }

    @Override
    protected void convert(BaseViewHolder helper, CollectionMediaWrapper item) {

        switch (helper.getItemViewType()) {
            case CollectionMediaWrapper.COMMON:
                CollectionMediaBean collectionMediaBean = item.getMediaBean();
                collectionMediaBean.__setDaoSession(DaoDbHelper.getInstance().getSession());
                helper.addOnClickListener(R.id.iv_select);
                if (!selectedStateEnabled) {
//                    ((ImageView) helper.getView(R.id.iv_select)).clearColorFilter();
//                    helper.setImageResource(R.id.iv_select, R.drawable.ic_baseline_more_vert_24);
//                    helper.setImageResource(R.id.iv_select, R.mipmap.icon_mine_not_select);
                    helper.setVisible(R.id.iv_select, false);
                } else {
                    helper.setVisible(R.id.iv_select, true);
                    if (selectedList.contains(item)) {
                        helper.setImageResource(R.id.iv_select, R.mipmap.icon_mine_has_selected);
                        ((ImageView) helper.getView(R.id.iv_select)).setColorFilter(mContext.getResources().getColor(R.color.colorAccent));
                    } else {
                        ((ImageView) helper.getView(R.id.iv_select)).clearColorFilter();
                        helper.setImageResource(R.id.iv_select, R.mipmap.icon_mine_not_select);
                    }
                }
//        helper.setVisible(R.id.iv_select, selectedStateEnabled);
                helper.setVisible(R.id.coll_book_iv_red_rot, collectionMediaBean.getIfUpdated());
                helper.setText(R.id.coll_book_tv_name, collectionMediaBean.getTitle());
//        helper.setVisible(R.id.iv_menu, true);
                if (collectionMediaBean.getIfLocal()) {
                    //本地文件的图片
                    Glide.with(mContext)
                            .load(R.mipmap.no_cover_portrait)
                            .fitCenter()
                            .into((ImageView) helper.getView(R.id.coll_book_iv_cover));
                } else {
                    //书的图片
                    helper.setText(R.id.coll_book_tv_source, "网源：" + collectionMediaBean.getSiteSourceBean().getSiteName());
                    Glide.with(mContext)
                            .load(collectionMediaBean.getCoverUrl())
                            .placeholder(R.mipmap.no_cover_portrait)
                            .fitCenter()
                            .into((ImageView) helper.getView(R.id.coll_book_iv_cover));
                }

                MediaRecordBean recordBean = MediaStorageManager.getInstance().getMediaRecord(collectionMediaBean.getId());
                int chapterIndex = recordBean == null ? 0 : recordBean.getChapter();
                List<MediaChapterBean> chapterBeanList = collectionMediaBean.getMediaChapterList();
                if (refreshingMediaSet.contains(collectionMediaBean.getId())) {
                    helper.setVisible(R.id.pb_chapter_loading, true);
                    helper.setVisible(R.id.tv_chapter_not_read, false);
                } else {

                    helper.setVisible(R.id.pb_chapter_loading, false);
                    helper.setVisible(R.id.tv_chapter_not_read, true);

                    int notReadYet = chapterBeanList.size() - chapterIndex - 1;
                    notReadYet = notReadYet < 0 ? 0 : notReadYet;
                    helper.setText(R.id.tv_chapter_not_read, notReadYet + "");
                }
                break;
            case CollectionMediaWrapper.GROUP:
                try {
                    List<CollectionMediaBean> mediaBeanList = item.getMediaGroupBo().getMediaBeanList();
                    helper.setVisible(R.id.iv_select, false);
                    helper.setText(R.id.tv_name, item.getMediaGroupBo().getName());
                    helper.setText(R.id.tv_num, "共" + mediaBeanList.size() + "本");
                    int[] ids = {R.id.iv_group_0, R.id.iv_group_1, R.id.iv_group_2, R.id.iv_group_3};
                    for(int i = 0; i < ids.length && i < mediaBeanList.size(); i++){
                        Glide.with(mContext)
                                .load(mediaBeanList.get(i).getCoverUrl())
                                .placeholder(R.mipmap.no_cover_portrait)
                                .fitCenter()
                                .into((ImageView) helper.getView(ids[i]));
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

                break;
        }

    }
}
